package com.myapplication.app;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.myapplication.app.fragment.FragmentBs;
import com.myapplication.app.fragment.FragmentChat;
import com.myapplication.app.fragment.FragmentMe;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by tukela on 2015/3/3.
 */
public class HomeActivity extends ActionBarActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager viewPager;
    private MyAdapter myFragmentAdapter;
    private List<Fragment> fragments=new ArrayList<Fragment>(0);
    private FragmentBs fragmentBs;
    private FragmentBs fragmentMsg;
    private TextView tv_Chat;
    private TextView tv_Msg;
    private TextView tv_Me;
    private TextView tv_Bus;
    private List<TextView> tvList=new ArrayList<TextView>(0);
    private FragmentMe fragmentMe;
    private FragmentChat fragmentChat;
    private MenuItem imgBar;
    private MenuItem imgPhoto;
    private MenuItem imgCamera;
    private MenuItem imgedit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        fragmentChat=new FragmentChat();
        fragmentMsg=new FragmentBs();
        fragmentMe=new FragmentMe();
        fragmentBs=new FragmentBs();
        fragments.add(fragmentChat);
        fragments.add(fragmentMsg);
        fragments.add(fragmentMe);
        fragments.add(fragmentBs);
//        fragments.add(prodectShopCartFragment);
//        fragments.add(orederProductHistoryFragment);
        myFragmentAdapter=new MyAdapter(getSupportFragmentManager());
        viewPager= (ViewPager) findViewById(R.id.product_viewpager);
        tv_Chat = (TextView) findViewById(R.id.chat);
        tv_Msg = (TextView) findViewById(R.id.msg);
        tv_Me = (TextView) findViewById(R.id.me);
        tv_Bus = (TextView) findViewById(R.id.bus);
        viewPager.setAdapter(myFragmentAdapter);
        viewPager.setOnPageChangeListener(this);
        tvList.add(tv_Chat);
        tvList.add(tv_Msg);
        tvList.add(tv_Me);
        tvList.add(tv_Bus);

        tv_Chat.setOnClickListener(this);
        tv_Msg.setOnClickListener(this);
        tv_Me.setOnClickListener(this);
        tv_Bus.setOnClickListener(this);
        tv_Chat.setTextColor(Color.YELLOW);
    }

//    /*   加载actionbar根据权限*/
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        MenuInflater inflater = getMenuInflater();
//        inflater.inflate(R.menu.menu_home, menu);
//        imgPhoto=menu.findItem(R.id.action_photo);
//        imgCamera=menu.findItem(R.id.action_camera);
//        imgedit=menu.findItem(R.id.action_edit);
//        return super.onCreateOptionsMenu(menu);
//
//    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        updateNavigation(i);
//        if (i==0)
//        {
//            imgPhoto.setVisible(true);
//            imgCamera.setVisible(true);
//            imgedit.setVisible(false);
//        }
//        else {
//            imgPhoto.setVisible(false);
//            imgCamera.setVisible(false);
//            imgedit.setVisible(false);
//        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    /*根据导航加载不同framgnent*/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.chat:
                viewPager.setCurrentItem(0);
                onPageSelected(0);
                updateNavigation(0);
                break;
            case  R.id.msg:
                viewPager.setCurrentItem(1);
                onPageSelected(1);
                updateNavigation(1);
                break;
            case  R.id.me:
                viewPager.setCurrentItem(2);
                onPageSelected(2);
                updateNavigation(2);
                break;
            case  R.id.bus:
                viewPager.setCurrentItem(3);
                onPageSelected(3);
                updateNavigation(3);
                break;


        }
    }

    class MyAdapter extends FragmentPagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
    /*******
     * 跟新导航背景
     * @param i 第i 个选项
     */
    private void updateNavigation(int i){
        for (int a=0;a<tvList.size();a++){
            if (a==i){
                TextView textView=tvList.get(a);
                // textView.setBackgroundColor(Color.BLUE);
                textView.setTextColor(Color.YELLOW);
              //  textView.setTextColor(R.color.black);
                //textView.setBackgroundResource(R.drawable.navigationselsect);
            }else {
                TextView textView=tvList.get(a);
                textView.setTextColor(Color.WHITE);
            }
        }
    }
}

